package msu.ml.core.xml;

import msu.ml.core.*;
import msu.ml.data.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

/**
 * @author Reginald M Mead
 * @version 1.0
 */
public class CrossValidationDataProviderParser implements IDataProviderParser
{
    public IDataProvider parse(Element element)
    {
        boolean clearCache = false;
        if(element.hasAttribute("clear_cache"))
            clearCache = Boolean.parseBoolean(element.getAttribute("clear_cache"));
        
        String database = "nexrad";
        String user = "rmead";
        String pass = "";

        if(element.hasAttribute("db_name"))
            database = element.getAttribute("db_name");

        if(element.hasAttribute("db_user"))
            user = element.getAttribute("db_user");

        if(element.hasAttribute("db_pass"))
            pass = element.getAttribute("db_pass");

        DbCrossValidationDataProvider provider = 
            new DbCrossValidationDataProvider("jdbc:mysql://localhost/" + database, user, pass, clearCache);

        int filesPerFold = Integer.parseInt(
                element.getAttribute("files_per_fold"));
        int folds = Integer.parseInt(element.getAttribute("folds"));
        String directory = element.getAttribute("directory");
        boolean autoBalance = false;
        if(element.hasAttribute("auto_balance"))
            autoBalance = Boolean.parseBoolean(element.getAttribute("auto_balance"));

        provider.setAutoBalance(autoBalance);
        provider.setNumFolds(folds);
        provider.setFilesPerFold(filesPerFold);
        provider.setDirectoryPath(directory);


        NodeList files = element.getElementsByTagName("file");
        for(int i = 0; i < files.getLength(); i++)
            provider.getFiles().add(files.item(i).getTextContent());

        if(ApplicationSettings.getInstance().getConfig("verbose").equals("true"))
        {
            System.out.println("Cross Validation Data Provider");
            System.out.println("\t Use " + filesPerFold + " files per fold");
            System.out.println("\t Use " + folds + " folds");
            System.out.println("\t Use Directory " + directory);
        }

        return provider;
    }
}
